package com.mall.biz.trade;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.mall.biz.pay.PayBizImpl;
import com.mall.biz.user.record.MallItemRecordBizImpl;
import com.mall.client.trade.TradeService;
import com.mall.client.user.UserExtendedService;
import com.mall.common.util.PageUtil;
import com.mall.pojo.Trade;
import com.mall.pojo.TradeExample;
import com.mall.pojo.TradeExample.Criteria;
import com.mall.pojo.User;
import com.mall.pojo.UserExtended;
import com.mall.query.Page;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.item.MallItemOrderVO;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;

@Service
public class TradeBizImpl {
	@Autowired
	private MallItemRecordBizImpl mallItemRecordBiz;
	@Autowired 
	private TradeService tradeService;
	@Autowired 
	private UserExtendedService userExtendedService;
	@Autowired 
	private PayBizImpl payBizImpl;
	/**
	 * 根据trade的query对象获得exampl对象
	 * @param tradeRecordQuery
	 * @return
	 */
	public TradeExample getTradeExampleByQuery(TradeRecordQuery tradeRecordQuery){
		TradeExample tradeExample = new TradeExample();
		if(tradeRecordQuery.getPage() != null){
			tradeExample.setStart(tradeRecordQuery.getPage().getBegin());
			tradeExample.setLimit(tradeRecordQuery.getPage().getLength());
		}
		if(tradeRecordQuery.getOrderby() != null){
			tradeExample.setOrderByClause(tradeRecordQuery.getOrderby());
		}
		
		Criteria criteria = tradeExample.createCriteria();
		if(tradeRecordQuery.getMallId() != null && tradeRecordQuery.getMallId() > 0){
			criteria.andMallIdEqualTo(tradeRecordQuery.getMallId());
		}
		if(tradeRecordQuery.getUserId() != null && tradeRecordQuery.getUserId() > 0){
			criteria.andBuyUserIdEqualTo(tradeRecordQuery.getUserId());
		}
		if(tradeRecordQuery.getOnlyKey() != null && tradeRecordQuery.getOnlyKey() > 0){
			criteria.andCommOnlyKeyEqualTo(tradeRecordQuery.getOnlyKey());
		}
		if(tradeRecordQuery.getType() != null){
			criteria.andTypeEqualTo(tradeRecordQuery.getType());
		}
		if(tradeRecordQuery.getBuyItemType() != null){
			criteria.andBuyItemTypeEqualTo(tradeRecordQuery.getBuyItemType());
		}
		if(tradeRecordQuery.getStatus() != null){
			criteria.andStatusEqualTo(tradeRecordQuery.getStatus());
		}
		if(tradeRecordQuery.getEndTime() != null && tradeRecordQuery.getStartTime() != null){
			criteria.andGmtModifyBetween(tradeRecordQuery.getStartTime(), tradeRecordQuery.getEndTime());
			//criteria.andGmtCreateLessThanOrEqualTo(tradeRecordQuery.getEndTime());
		}
//		if(tradeRecordQuery.getStartTime() != null){
//			criteria.andGmtCreateGreaterThanOrEqualTo(tradeRecordQuery.getStartTime());
//		}
		return tradeExample;
	}
	
	/**
	 * 订单列表Map
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	public ModelMap tradeRecordMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize",PageUtil.PAGE_SIZE_APP);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		User user = WebUtil.getCurrentUser(request);
		TradeRecordQuery tradeRecordQuery = new TradeRecordQuery();
		tradeRecordQuery.setOnlyKey(onlyKey);
		if(user != null){
			tradeRecordQuery.setUserId(user.getId());
		}
		tradeRecordQuery.setPage(page);
		tradeRecordQuery.setStatus(status);
		List<MallItemOrderVO> mallItemOrderVOList = this.mallItemRecordBiz.getMallItemOrderByQuery(tradeRecordQuery);
		modelMap.put("mallItemOrderVOList", mallItemOrderVOList);
		int app = APPCookieUtil.urlFromApp(request);
		modelMap.put("app", app);
		return modelMap;
	}
	
	/**
	 * 分销商确认收货并升级
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	public boolean updateTrade(Long tradeId,TradeRecordQuery tradeRecordQuery) {
		if (tradeRecordQuery.getUserId() == null || tradeId == null) {
			return false;
		}
		
		Trade  trade = this.tradeService.getTradeByOrderId(tradeId);
		trade.setStatus(4);
		trade.setLogisticsStatus(4);
		trade.setGmtModify(new Date());
		boolean result = this.tradeService.updateById(tradeId, trade);
		if(result==false){
			return false;
		}
	/*	TradeExample  example = getTradeExampleByQuery(tradeRecordQuery);
		Long sumPrice = this.tradeService.getSumPriceByExample(example); 
		User user = this.userService.getUserByUserId(tradeRecordQuery.getUserId());
		UserLevel userLevel = this.userLevelService.getUserLevelByPrice(sumPrice,tradeRecordQuery.getOnlyKey());
		if(userLevel == null){
			user.setLevel((short) 0);
		}
		if(userLevel != null && userLevel.getLevel().shortValue() > user.getLevel()){
			user.setLevel(userLevel.getLevel().shortValue());
		}
		
		user.setGmtModify(new Date());
		this.userService.modifytUserByPrimaryKey(user);*/
		return true;
		
	}
	
	/**
	 * 付款扣与额中的钱
	 * @param request
	 * @param orderId
	 * @return
	 */
	public String balancePay(HttpServletRequest request, Long orderId){
		User user = WebUtil.getCurrentUser(request);
		Trade trade = this.tradeService.getTradeByOrderId(orderId);
		if(trade == null){
			return null;
		}
		if(trade.getVersion() == 1){
			UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(user.getId());
			if(userExtended.getBalance() < trade.getPrice()){
				return "weishang/trade/noMoneyError" ;
			}
			userExtended.setBalance(userExtended.getBalance()-trade.getPrice());
			userExtended.setGmtModify(new Date());
			this.userExtendedService.updateExtendedByPrimaryKey(userExtended);
			trade.setStatus(1);
			trade.setVersion(2);
			this.tradeService.updateTradeByTrade(trade);
			this.payBizImpl.paySuccessCallback(trade.getId());
			return "weishang/trade/orderSuccess";
		}else if(trade.getVersion() == 2){
			return "weishang/trade/orderSuccess";
		}
		return "weishang/trade/orderError";
	}
}
